//手机号码正则表达式
var phoneReg=/^1[345789][0-9]{9}$/;
//验证码正则表达式
var codeReg = /^[0-9]{6}$/;
var pwdReg0 = /^.{6,16}$/;
var pwdReg1 = /[0-9]+/;
var pwdReg2 = /[a-zA-Z]+/;
var pwdReg3 = /[=~!@#\$%\^&\*\(\)_\+\{\[\}\]:"\|',<?.\/\\]+/;
var timer;
var t = 60;
//表示是否同意注册协议
var isAgree = false;
var isPhoneRight = false;
var isYZMRight = false;
var isPwdRight = false;
var phoneerror = document.getElementById("phoneerror");
var phone = document.getElementById("phone");
var yzmBtn = document.getElementById("yzmBtn");
var eye = document.getElementById("eye");
var flag = 0;
var btn = document.getElementById("btn");
var newpwd = document.getElementById("newpwd");

phone.onblur= function(){
    var phoneValue = phone.value;
    if(!phoneReg.test(phoneValue)){
        phoneerror.innerHTML = "<img src='images/error.png'/>";
        isPhoneRight = false;
    }else{
        phoneerror.innerHTML = "<img src='images/green.png'/>";
        isPhoneRight = true;
    }
    checkIsHavePhone();
};

function checkIsHavePhone(){
    $.get("http://test.hfjava.com/shop/userphone?phone="+phone.value,{},function(data){
        if(data=="true"){
            yzmBtn.setAttribute("disabled","disabled");
            phoneerror.innerHTML ="<img src='images/error.png'/>";
            isPhoneRight = true;

        }
    });
}

phone.onkeyup = function(){
    var phoneValue = phone.value;
    if(!phoneReg.test(phoneValue)){
        yzmBtn.setAttribute("disabled","disabled");
        yzmBtn.style.color = "";
        isPhoneRight = false;
    }else{
        yzmBtn.removeAttribute("disabled");
        yzmBtn.style.color = "#f2a13e";
        isPhoneRight = true;
    }

};

yzmBtn.onclick = function(){
    yzmBtn.setAttribute("disabled","disabled");
    yzmBtn.style.color = "";
    yzmBtn.innerHTML = "重新发送60";
    timer = setInterval(daojishi,1000);
};
function daojishi(){
    t--;
    if(t==0){
        clearInterval(timer);
        yzmBtn.innerHTML = "发送验证码";
        yzmBtn.style.color = "#f2a13e";
        t = 60;
        return;
    }
    yzmBtn.innerHTML = t+"s";

}
//功能4：验证验证码正确性
yzm.onblur = function(){
    var yzmValue = yzm.value;
    if(yzmValue.length==0){
        yzmerror.inneHTML = "请您输入验证码！";
        isYZMRight = false;
        checkAll();
        return;
    }
    console.log(codeReg.test(yzmValue));
    if(!codeReg.test(yzmValue)){
        yzmerror.innerHTML = "<img src='images/error.png'/>";
        isYZMRight = false;
        checkAll();
        return;
    }
    $.ajax({
        url:"http://test.hfjava.com/shop/yzm",
        dataType:"jsonp",
        success:function(data){

            if(data.code!=yzmValue){
                yzmerror.innerHTML = "<img src='images/error.png'/>";
                isYZMRight = false;
            }else{
                yzmerror.innerHTML = "<img src='images/green.png'/>";
                isYZMRight = true;

            }


        }
    })
};

newpwd.onkeyup = function(){
    var newpwdValue = newpwd.value;
    var rightCount = 0;
    //长度 字母 数字 符号
    var lengthValid = pwdReg0.test(newpwdValue);
    var numberValid = pwdReg1.test(newpwdValue);
    var letterValid = pwdReg2.test(newpwdValue);
    var symbolValid = pwdReg3.test(newpwdValue);

    if(numberValid){
        rightCount++;
    }
    if(letterValid){
        rightCount++;
    }
    if(symbolValid){
        rightCount++;
    }
    if(!lengthValid||rightCount<2){
        pwderror.innerHTML = "<img src='images/error.png'/>";
        isPwdRight = false;
    }else{
        pwderror.innerHTML = "<img src='images/green.png'/>";
        isPwdRight = true;
    }


};
eye.onclick = function(){
    var password = $("#newpwd").val();
    if(password == null || password == ""){
        alert("请检查用户密码");
        return;
    }else if(flag == 0){
        newpwd.type="";
        flag = 1;
    }else if(flag == 1){
        newpwd.type="password";
        flag = 0;
    }

};

btn.onclick = function(){
    setNewPwd()
};
function setNewPwd(){
    $.post("http://test.hfjava.com/shop/updateuserpwd",
        {phone:phone.value
            ,newPwd:newpwd.value},function(data){
            if(data=="Saved"){
                alert("修改密码成功！");
                setTimeout(denglu,1000);
            }
        });
}

function denglu(){
    location.href = "login_page.html";
}